package com.egan.pet.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egan.pet.admin.constant.StatusConstant;
import com.egan.entity.dto.ItemPageQueryDTO;
import com.egan.entity.dto.ProductDTO;
import com.egan.entity.po.Item;
import com.egan.entity.vo.ProductVO;
import com.egan.pet.admin.repository.mapper.ItemMapper;
import com.egan.pet.admin.result.PageResult;
import com.egan.pet.admin.service.IItemService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author egan
 * @since 2024-09-10
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Resource
    private ItemMapper itemMapper;

    @Override
    public ProductVO getByIdWithDesc(long id) {

        Item item = itemMapper.getById(id);

        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(item, productVO);
        return productVO;
    }

    /**
     * 保存产品
     *
     * @param productDTO
     */
    @Override
    public void saveItem(ProductDTO productDTO) {
        Item item = new Item();
        BeanUtils.copyProperties(productDTO, item);
        itemMapper.insert(item);
    }

    @Override
    public PageResult pageQuery(ItemPageQueryDTO itemPageQueryDTO) {
        PageHelper.startPage(itemPageQueryDTO.getPage(), itemPageQueryDTO.getPageSize());
        Page<ProductVO> page = itemMapper.pageQuery(itemPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }


    @Override
    public void updateId(ProductDTO productDTO) {
        Item item = new Item();
        BeanUtils.copyProperties(productDTO,item);
        itemMapper.insertOrUpdate(item);
    }

    @Override
    public List<Item> queryCategoryId(Long cid) {
        Item item = Item.builder()
                .cid(cid)
                .status(StatusConstant.ENABLE)
                .build();

        return itemMapper.list(item);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Item item = Item.builder()
                .id(id)
                .status(status)
                .build();
        itemMapper.updateStatus(item);
    }
}
